package com.small.base.redis.core;

import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * StringRedisService
 *
 * @author miajio
 */
public class StringRedisService implements RedisInterface {

    private StringRedisTemplate stringRedisTemplate;

    public StringRedisService(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @Override
    public boolean expire(String key, long time, TimeUnit timeUnit) {
        return stringRedisTemplate.expire(key, time, timeUnit);
    }

    @Override
    public long getTime(String key, TimeUnit timeUnit) {
        return stringRedisTemplate.getExpire(key, timeUnit);
    }

    @Override
    public long getTime(String key) {
        return stringRedisTemplate.getExpire(key);
    }

    @Override
    public boolean hasKey(String key) {
        return stringRedisTemplate.hasKey(key);
    }

    @Override
    public boolean removeExpire(String key) {
        return stringRedisTemplate.boundValueOps(key).persist();
    }

    @Override
    public void set(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void set(String key, String value, long time, TimeUnit timeUnit) {
        if (time > 0) {
            stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
        } else {
            stringRedisTemplate.opsForValue().set(key, value);
        }
    }

    @Override
    public void batchSet(Map<String, String> keyAndValueSet) {
        stringRedisTemplate.opsForValue().multiSet(keyAndValueSet);
    }

    @Override
    public void batchSetIfAbsent(Map<String, String> keyAndValueSet) {
        stringRedisTemplate.opsForValue().multiSetIfAbsent(keyAndValueSet);
    }

    @Override
    public Object get(String key) {
        if (null != key) {
            return stringRedisTemplate.opsForValue().get(key);
        }
        return null;
    }

    public StringRedisTemplate getStringRedisTemplate() {
        return this.stringRedisTemplate;
    }
}
